package initialize

import (
	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"server/pkg/setting"
)

func InitCasbin() {
	a, err := gormadapter.NewAdapter("mysql", "root:abc@123@tcp(127.0.0.1:3306)/")
	if err != nil {
		log.Fatalf("error: adapter: %s", err)
	}
	setting.Casbin, err = casbin.NewEnforcer("configs/casbin.conf", a)
	if err != nil {
		log.Fatalf("error: adapter: %s", err)
	}
	err = setting.Casbin.LoadPolicy()
	if err != nil {
		log.Fatalf("error: adapter: %s", err)
	}
	setting.Casbin.Enforce("alice", "data1", "read")
	setting.Casbin.SavePolicy()
}
